package com.tsd.jxc.fin.entity;

import com.tsd.core.utils.DoubleUtil;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.utils.ListUtil;
import com.tsd.jxc.fin.FinanceConstants;
import com.tsd.jxc.fin.vo.PaymentRequestDetailVo;
import com.tsd.jxc.fin.vo.PaymentRequestExportVo;
import com.tsd.jxc.req.JxcFinancePaymentRequestDetailReqParamsVo;
import com.tsd.jxc.setting.JxcSettingUtil;
import com.tsd.jxc.utils.ClothingUtil;
import com.tsd.jxc.vo.JxcSettingVo;
import io.swagger.annotations.ApiModel;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * 财务-收付款请求单扩展
 *
 * @author Hillpool
 * @date 2023/02/09 18:00:21
 */
@ApiModel(description = "财务-收付款请求单扩展")
public class JxcFinancePaymentRequestExt extends JxcFinancePaymentRequest implements Serializable {

    private String client_name;

    private String client_code;

    private String client_short_name;

    private String client_display;

    private String supplier_name;

    private String supplier_code;

    private String supplier_short_name;

    private String supplier_display;

    private String group_name;

    private String creator_person_name;

    private String auditor_person_name;

    private String source;

    private List<String> sid_list;

    private List<JxcFinancePaymentRequestDetailExt> detail_list;

    public String getClient_name() {
        return client_name;
    }

    public void setClient_name(String client_name) {
        this.client_name = client_name;
    }

    public String getClient_code() {
        return client_code;
    }

    public void setClient_code(String client_code) {
        this.client_code = client_code;
    }

    public String getClient_short_name() {
        return client_short_name;
    }

    public void setClient_short_name(String client_short_name) {
        this.client_short_name = client_short_name;
    }

    public String getClient_display() {
        return client_display;
    }

    public void setClient_display(String client_display) {
        this.client_display = client_display;
    }

    public String getSupplier_name() {
        return supplier_name;
    }

    public void setSupplier_name(String supplier_name) {
        this.supplier_name = supplier_name;
    }

    public String getSupplier_code() {
        return supplier_code;
    }

    public void setSupplier_code(String supplier_code) {
        this.supplier_code = supplier_code;
    }

    public String getSupplier_short_name() {
        return supplier_short_name;
    }

    public void setSupplier_short_name(String supplier_short_name) {
        this.supplier_short_name = supplier_short_name;
    }

    public String getSupplier_display() {
        return supplier_display;
    }

    public void setSupplier_display(String supplier_display) {
        this.supplier_display = supplier_display;
    }

    public String getGroup_name() {
        return group_name;
    }

    public void setGroup_name(String group_name) {
        this.group_name = group_name;
    }

    public String getCreator_person_name() {
        return creator_person_name;
    }

    public void setCreator_person_name(String creator_person_name) {
        this.creator_person_name = creator_person_name;
    }

    public String getAuditor_person_name() {
        return auditor_person_name;
    }

    public void setAuditor_person_name(String auditor_person_name) {
        this.auditor_person_name = auditor_person_name;
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    public List<String> getSid_list() {
        return sid_list;
    }

    public void setSid_list(List<String> sid_list) {
        this.sid_list = sid_list;
    }

    public List<JxcFinancePaymentRequestDetailExt> getDetail_list() {
        return detail_list;
    }

    public void setDetail_list(List<JxcFinancePaymentRequestDetailExt> detail_list) {
        this.detail_list = detail_list;
    }

    public PaymentRequestExportVo convertExportVo() {
        PaymentRequestExportVo exportVo = new PaymentRequestExportVo();
        if (FinanceConstants.TYPE_SALE.equals(this.getType())) {
            exportVo.setClient_supplier_name(this.getClient_name());
        } else {
            exportVo.setClient_supplier_name(this.getSupplier_name());
        }
        String beginDateStr = HlpUtils.date2String(this.getBegin_date(), HlpUtils.Format_Yyyy_M_d_Cn);
        String endDateStr = HlpUtils.date2String(this.getEnd_date(), HlpUtils.Format_Yyyy_M_d_Cn);
        exportVo.setCycle_name(beginDateStr + "至" + endDateStr);
        exportVo.setPre_end_amount(this.getPre_end_amount());
        exportVo.setAmount(this.getAmount());
        exportVo.setPayment_amount(this.getPayment_amount());
        exportVo.setEnd_amount(this.getEnd_amount());
        exportVo.setNote_info(this.getNote_info());
        JxcSettingVo settingVo = JxcSettingUtil.getSettingVo();
        List<PaymentRequestDetailVo> detailVoList = new ArrayList<>();
        if (settingVo.isClothing()) {
            List<JxcFinancePaymentRequestDetailReqParamsVo> voList = JxcFinancePaymentRequestDetailReqParamsVo.transfer(this.getDetail_list(), settingVo);
            if (!HlpUtils.isEmptyList(voList)) {
                List<String> unionSizeList = new ArrayList<>();
                for (JxcFinancePaymentRequestDetailReqParamsVo paramsVo : voList) {
                    List<String> filterSizeList = ListUtil.filter(paramsVo.getSpec_preset_value_list(), t -> !unionSizeList.contains(t));
                    if (!HlpUtils.isEmptyList(filterSizeList)) {
                        unionSizeList.addAll(filterSizeList);
                    }
                }
                List<String> sizeGroupList = ClothingUtil.getClothing_preset_size_group_list(settingVo);
                List<String> filterSizeGroupList = ListUtil.filter(sizeGroupList, sizeGroup -> ListUtil.anyMatch(unionSizeList, t -> String.format(",%s,", sizeGroup).contains(String.format(",%s,", t))));
                int maxSize = 0;
                for (String sizeGroup : filterSizeGroupList) {
                    int len = sizeGroup.split(",").length;
                    if (maxSize < len) {
                        maxSize = len;
                    }
                }
                exportVo.setSize(maxSize);
                Map<String, Object> sizeMap = new HashMap<>();
                Map<String, Object> sizeMap2 = new HashMap<>();
                for (int i = 0; i < maxSize; i++) {
                    List<String> sizeStrList = new ArrayList<>();
                    for (String sizeGroup : filterSizeGroupList) {
                        String[] strings = sizeGroup.split(",");
                        if (i < strings.length) {
                            sizeStrList.add(strings[i]);
                        }
                    }
                    sizeMap2.put("preset_size_" + i, StringUtils.join(sizeStrList, "/"));
                    sizeMap.put("preset_size_" + i, StringUtils.join(sizeStrList, "/"));
                    String qtyField = "t.preset_qty_map.preset_qty_" + i;
                    if (sizeStrList.size() == 1) {
                        qtyField = "n:" + qtyField;
                    }
                    sizeMap.put("preset_qty_" + i, qtyField);
                }
                sizeMap.put("qty", "n:t.qty");
                sizeMap.put("price", "n:t.price");
                sizeMap.put("amount", "n:t.amount");
                sizeMap.put("payment_amount", "n:t.payment_amount");
                sizeMap.put("remain_amount", "n:t.remain_amount");
                sizeMap.put("note_info", "t.note_info}}");
                exportVo.setSize_map(sizeMap2);
                exportVo.setSize_list(Collections.singletonList(sizeMap));
                for (JxcFinancePaymentRequestDetailReqParamsVo paramsVo : voList) {
                    PaymentRequestDetailVo detailVo = paramsVo.convertExportVo();
                    List<String> sizeList = ClothingUtil.getClothing_preset_size_list(settingVo);
                    Map<String, String> presetQtyMap = new HashMap<>();
                    for (int i = 0; i < maxSize; i++) {
                        List<String> qtyStrList = new ArrayList<>();
                        for (String sizeGroup : filterSizeGroupList) {
                            String[] strings = sizeGroup.split(",");
                            if (i < strings.length) {
                                String size = strings[i];
                                int index = ListUtil.indexOf(sizeList, t -> t.equals(size));
                                if (index == -1) {
                                    continue;
                                }
                                BigDecimal qty = paramsVo.getPreset_qty_map().get("preset_qty_" + index);
                                if (qty == null) {
                                    qty = BigDecimal.ZERO;
                                }
                                qtyStrList.add(qty.stripTrailingZeros().toPlainString());
                            }
                        }
                        presetQtyMap.put("preset_qty_" + i, StringUtils.join(qtyStrList, "/"));
                    }
                    detailVo.setPreset_qty_map(presetQtyMap);
                    detailVoList.add(detailVo);
                }
            }
        } else {
            detailVoList = ListUtil.map(this.getDetail_list(), JxcFinancePaymentRequestDetailExt::convertExportVo);
        }
        double totalQty = 0D, remainAmount = exportVo.getPre_end_amount().doubleValue();
        if (!HlpUtils.isEmptyList(detailVoList)) {
            for (PaymentRequestDetailVo vo : detailVoList) {
                totalQty = DoubleUtil.add(totalQty, vo.getQty());
                remainAmount = DoubleUtil.add(remainAmount, vo.getAmount());
                vo.setRemain_amount(BigDecimal.valueOf(remainAmount));
            }
        }
        exportVo.setTotal_qty(BigDecimal.valueOf(totalQty));
        exportVo.setDetail_list(detailVoList);
        return exportVo;
    }
}